﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core;
using FluentValidation.Results;

namespace Micrhard.FMCG.Server.Validator
{
    public class SaleLeadModelValidator : AbstractValidator<SaleLeadModel>
    {
        public SaleLeadModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Customer.Name).NotEmpty().WithMessage(SaleLeadMsg.NeedName).When(r => r.SaleLeadID == Guid.Empty);
                RuleFor(u => u.Customer.Address).NotEmpty().WithMessage(SaleLeadMsg.NeedAddress).When(r => r.SaleLeadID == Guid.Empty);
                RuleFor(u => u.Customer.Cellphone).NotEmpty().WithMessage(SaleLeadMsg.NeedCellphone).When(r => r.SaleLeadID == Guid.Empty);
                RuleFor(u => u.Description).NotEmpty().WithMessage(SaleLeadMsg.NeedLeadDescrpition).When(r => r.SaleLeadID == Guid.Empty);

                Custom(u =>
                {
                    ICustomer ict = IoC.Resolve<ICustomer>();
                    if (ict.IsExist(u.Customer))
                    {
                        return new ValidationFailure("Customer.Name", SaleLeadMsg.DuplicateCustomer);
                    }
                    return null;
                });

            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.Description).NotEmpty().WithMessage(SaleLeadMsg.NeedLeadDescrpition);

                Custom(u =>
                {
                    if (u.OldCustomerID == u.CustomerID)
                    {
                        return new ValidationFailure("OldCustomerID", SaleLeadMsg.OldCusIDIsSelf);
                    }
                    return null;
                });
            });
        }
    }
}
